/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package halali;

import comparator.Comparator;
import java.util.List;
import java.util.Objects;

/**
 *
 * @author martin100
 */
public class TitleMatcher {

    private List<CSFDTitle> titles;
    private final ShowTitle showTitle;
    private double max = 0;
    private Comparator comp = new Comparator();

    public TitleMatcher(List<CSFDTitle> titles, ShowTitle showTitle) {
        Objects.requireNonNull(titles);
        this.titles = titles;
        this.showTitle = showTitle;
    }

    public Integer findMatch() {
        Integer resultId = null;
        for (CSFDTitle csfdTitle : titles) {
            if (match(csfdTitle.title)) {
                double tmp = compare(csfdTitle.title);
                if (tmp > max) {
                    max = tmp;
                    resultId = csfdTitle.movieId;
                }
            }
        }
        return resultId;
    }

    private boolean match(String csfdTitle) {
        return comp.match(csfdTitle, showTitle.originalTitle) || comp.match(csfdTitle, showTitle.reducedTitle);
    }

    private double compare(String csfdTitle) {
        double a1 = comp.compareStrings(csfdTitle, showTitle.originalTitle);
        double a2 = comp.compareStrings(csfdTitle, showTitle.reducedTitle);
        return a1 > a2 ? a1 : a2;
    }

}
